# coding:utf-8
import jieba.posseg
import random
import re


def delete_random(line):
    pox = ['uj', 'u', 'ud', 'vn', 'ul', 'd', 'ad', 'dg', 'y', 'z', 'uv', 'uz', 'vd']
    lines = jieba.posseg.cut(line)
    list_r = []
    # 对列表循环同时获取索引
    for num, word in enumerate(lines):
        if word.flag in pox:
            # 获取要删除的词性的索引集合
            list_r.append(num)
        # print(num, word, word.flag)
    # 定义最终需要删除的随机索引集合
    list_need_delete = []
    # 随机选择一半的弱词性的删除
    list_random = random.sample(range(0, len(list_r)), len(list_r) // 2)
    for k in list_random:
        list_need_delete.append(list_r[k])
    # print(list_r, len(list_r))
    # print(list_random, type(list_random))
    out = ""
    # print(list_need_delete, len(list_r))
    newlines = jieba.posseg.cut(line)
    for num2, word2 in enumerate(newlines):
        if num2 not in list_need_delete:
            out += word2.word
    return out


def handle(infile, outfile):
    fp = open(infile, 'r', encoding='utf-8')
    fout = open(outfile, 'w', encoding='utf-8')
    i = 0
    for line in fp.readlines():
        i += 1
        line = line.strip()
        # 奇数位
        if i % 2 != 0:
            fout.write('%s\n' % line)
        # 偶数位 生成的句子
        else:
            if bool(re.search("[-\d()*+/=\.]{10,}", line)):
                fout.write('%s\n' % line)
                print("这行不加噪：",i)
                continue
            result = delete_random(line)
            fout.write('%s\n' % result)


if __name__ == '__main__':
    infile = 'g:/outfile2.txt'
    outfile = 'g:/noise.txt'
    handle(infile, outfile)
